#include "Model.h"
#include "soldier25.h"

Model::Model(Ptr< ShowListManager > showListMng,
             const WCHAR* modelFileName)
{
  _load_model (showListMng, modelFileName);
}

void Model::draw (Ptr< Object > object)
{
  glPushMatrix ();

  // Draw country of object if exists
  if (_countryShowList != 0)
  {
    glColor3fv (object->owner ().color.data ());
    glCallList (_countryShowList);
  }

  // Draw object itself
  glCallList (_modelShowList);

  if (object->selected())
	  glCallList (_statusShowList);

  glPopMatrix ();

}

const Size3f& Model::size () const
{
  return _size;
}

void Model::_load_model (Ptr< ShowListManager > showListMng,
                         const WCHAR* modelFileName)
{
  if (modelFileName == NULL) // Not specified model, create default
  {
    // Set default size
    _size = Size3f (2, 2, 0.5);

    // Draw object
    _modelShowList = showListMng->createNewList (GL_COMPILE);
/*
	for( int i = 0 ; i< soldier25NumVerts ;i++ ){
		glBegin(GL_TRIANGLES);
		glNormal3f(soldier25Verts[i*3] , soldier25Verts[i*3+1] , soldier25Verts[i*3+2]);
		glVertex3f( soldier25Verts[i*3] , soldier25Verts[i*3+1] , soldier25Verts[i*3+2]);
		i++;
		glNormal3f(soldier25Verts[i*3] , soldier25Verts[i*3+1] , soldier25Verts[i*3+2]);
		glVertex3f( soldier25Verts[i*3] , soldier25Verts[i*3+1] , soldier25Verts[i*3+2]);
		i++;
		glNormal3f(soldier25Verts[i*3] , soldier25Verts[i*3+1] , soldier25Verts[i*3+2]);
		glVertex3f( soldier25Verts[i*3] , soldier25Verts[i*3+1] , soldier25Verts[i*3+2]);
		glEnd();
	}
*/
    glPushAttrib (GL_COLOR_BUFFER_BIT);
    
    
/*
    glBegin (GL_QUADS);

    glNormal3f (-1.0, -1.0, -1.0);
    glVertex3f (-_size[0] / 2, -_size[1] / 2, 0);
    glNormal3f (1.0, -1.0, -1.0);
    glVertex3f (_size[0] / 2, -_size[1] / 2, 0);
    glNormal3f (1.0, 1.0, -1.0);
    glVertex3f (_size[0] / 2, _size[1] / 2, 0);
    glNormal3f (-1.0, 1.0, -1.0);
    glVertex3f (-_size[0] / 2, _size[1] / 2, 0);

    glNormal3f (-1.0, -1.0, 1.0);
    glVertex3f (-_size[0] / 2, -_size[1] / 2, _size[2]);
    glNormal3f (1.0, -1.0, 1.0);
    glVertex3f (_size[0] / 2, -_size[1] / 2, _size[2]);
    glNormal3f (1.0, 1.0, 1.0);
    glVertex3f (_size[0] / 2, _size[1] / 2, _size[2]);
    glNormal3f (-1.0, 1.0, 1.0);
    glVertex3f (-_size[0] / 2, _size[1] / 2, _size[2]);

    glNormal3f (-1.0, -1.0, -1.0);
    glVertex3f (-_size[0] / 2, -_size[1] / 2, 0);
    glNormal3f (1.0, -1.0, -1.0);
    glVertex3f (_size[0] / 2, -_size[1] / 2, 0);
    glNormal3f (1.0, -1.0, 1.0);
    glVertex3f (_size[0] / 2, -_size[1] / 2, _size[2]);
    glNormal3f (-1.0, -1.0, 1.0);
    glVertex3f (-_size[0] / 2, -_size[1] / 2, _size[2]);

    glNormal3f (-1.0, 1.0, -1.0);
    glVertex3f (-_size[0] / 2, _size[1] / 2, 0);
    glNormal3f (1.0, 1.0, -1.0);
    glVertex3f (_size[0] / 2, _size[1] / 2, 0);
    glNormal3f (1.0, 1.0, 1.0);
    glVertex3f (_size[0] / 2, _size[1] / 2, _size[2]);
    glNormal3f (-1.0, 1.0, 1.0);
    glVertex3f (-_size[0] / 2, _size[1] / 2, _size[2]);

    glNormal3f (-1.0, 1.0, -1.0);
    glVertex3f (-_size[0] / 2, _size[1] / 2, 0);
    glNormal3f (-1.0, -1.0, -1.0);
    glVertex3f (-_size[0] / 2, -_size[1] / 2, 0);
    glNormal3f (-1.0, -1.0, 1.0);
    glVertex3f (-_size[0] / 2, -_size[1] / 2, _size[2]);
    glNormal3f (-1.0, 1.0, 1.0);
    glVertex3f (-_size[0] / 2, _size[1] / 2, _size[2]);

    glNormal3f (1.0, 1.0, -1.0);
    glVertex3f (_size[0] / 2, _size[1] / 2, 0);
    glNormal3f (1.0, -1.0, -1.0);
    glVertex3f (_size[0] / 2, -_size[1] / 2, 0);
    glNormal3f (1.0, -1.0, 1.0);
    glVertex3f (_size[0] / 2, -_size[1] / 2, _size[2]);
    glNormal3f (1.0, 1.0, 1.0);
    glVertex3f (_size[0] / 2, _size[1] / 2, _size[2]);

    glEnd ();
*/
	glColor3f (1.0f, 1.0f, 1.0f);

	glLineWidth(1.0);
	glBegin(GL_LINES);
	glNormal3f(0,0,1);
	glVertex3f(0,0,0.5);
	glNormal3f(0,0,1);
	glVertex3f(0,5*_size[2],0.5);
	glEnd();

	glBegin(GL_TRIANGLES);
	glNormal3f(0,0,1);
	glVertex3f(0,2*_size[2],0.5);
	glNormal3f(0,0,1);
	glVertex3f(0,5*_size[2],0.5);
	glNormal3f(0,0,1);
	glVertex3f(2*_size[0],2*_size[2],0.5);
	glEnd();

    glPopAttrib ();

    glEndList ();

    // Draw country part (Color is determined in runtime)
    _countryShowList = showListMng->createNewList (GL_COMPILE);
/*
    glBegin (GL_QUADS);

    glNormal3f (-1.0, -1.0, 1.0);
    glVertex3f (-_size[0] / 2, -_size[1] / 2, _size[2]);
    glNormal3f (1.0, -1.0, 1.0);
    glVertex3f (_size[0] / 2, -_size[1] / 2, _size[2]);
    glNormal3f (1.0, 1.0, 1.0);
    glVertex3f (_size[0] / 2, _size[1] / 2, _size[2]);
    glNormal3f (-1.0, 1.0, 1.0);
    glVertex3f (-_size[0] / 2, _size[1] / 2, _size[2]);

    glEnd ();
*/
    glEndList ();
  
	_statusShowList = showListMng->createNewList (GL_COMPILE);

	glBegin (GL_QUADS);

	glColor3f (0.5f, 0.5f, 0.5f);

    glNormal3f(0,0,1);
    glVertex3f (_size[0]/4, -_size[1]/4, 0);
    glNormal3f(0,0,1);
    glVertex3f (_size[0]/4, _size[1]/4, 0);
    glNormal3f(0,0,1);
    glVertex3f (-_size[0]/4, _size[1]/4, 0);
    glNormal3f(0,0,1);
    glVertex3f (-_size[0]/4, -_size[1]/4, 0);

    glEnd ();

    glEndList ();
  }



  // Read Model
  // NOT DONE <--------------------------------------------
}